import time,pytest
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains


#def read_yaml():
#    with open('data.yaml','r',encoding='utf-8') as f:
#        return yaml.safe_load(f)
#datas = read_yaml()
#

@pytest.fixture(scope='function')
def driver():

    driver = webdriver.Chrome()
    driver.get('https://www.jiuxian.com/')
    driver.maximize_window()
    yield driver #测试执行
    time.sleep(2)
    driver.quit()
#@pytest.mark.parametrize('data',datas)
def test_jiuxianwang_01_login(driver):
    #点击登录页面
    denglu=driver.find_element(By.XPATH,'/html/body/div[2]/div/div[1]/div/a[1]')
    denglu.click()
    #强制等待
    time.sleep(5)
    #输入用户名密码
    username = driver.find_element(By.NAME,'uname')
    pwe = driver.find_element(By.NAME,'_pwd')
    username.send_keys("18615488780")
    pwe.send_keys("lr123456.")
    #验证码等待10秒
    time.sleep(10)

    #登录按钮
    dlbutt = driver.find_element(By.ID,'save')
    dlbutt.click()
    #隐式等待
    #driver.implicitly_wait(10)
    #显示等待
    #WebDriverWait(driver,20,0.5).until(Ec.presence_of_element_located((By.ID,'save')))
    time.sleep(5)
    #断言 文本白酒
    dy1 = driver.find_element(By.XPATH,'/html/body/div[4]/div/div[2]/div[2]/p/a[1]').text
    print('断言文本:',dy1)
    time.sleep(5)

    #在搜索框输入白酒 //*[@id="wd"]
    shousuo = driver.find_element(By.XPATH,'//*[@id="wd"]')
    shousuo.send_keys('白酒')
    time.sleep(3)
    #鼠标双击
    ActionChains(driver).double_click(shousuo).perform()
    time.sleep(2)
    #键盘 Tab
    shousuo.send_keys(Keys.TAB)
    time.sleep(3)
    #切窗口
    driver.switch_to.window(driver.window_handles[-1])
    ##进框架
    #driver.switch_to.frame(driver.find_element(By.XPATH,''))
    ##退框架
    #driver.switch_to.default_content()
    #浏览器刷新
    driver.refresh()
    #弹框处理
    try:
        alert=driver.switch_to.alert
        alert.accept()
        print('弹框已处理')
    except:
        print('无弹框出现')

    #下拉框选择处理 下拉框按
    #select_element=Select(driver.find_element(By.ID,''))
    #select_element.select_by_visible_text('')

    #点击搜索按钮 search-btn
    shousuo_butt = driver.find_element(By.CLASS_NAME,'search-btn')
    shousuo_butt.click()
    time.sleep(3)
    #搜索成功后 断言文本 五粮液//*[@id="brandAll"]/ul/li[1]/a

    dy2 = driver.find_element(By.XPATH,'//*[@id="brandAll"]/ul/li[1]/a').text
    print('断言文本',dy2)
    time.sleep(3)

if __name__ == '__main__':
    pytest.main()
